package my.mypool.core;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.impl.StackObjectPool;

import java.util.Map;
import java.util.HashMap;

/**
 * Created by IntelliJ IDEA.
 * User: Administrator
 * Date: 2010-9-20
 * Time: 11:36:17
 * To change this template use File | Settings | File Templates.
 */
public class PoolablePool {
    private ObjectPool pool = null;

    public PoolablePool(Class poolableClass, Object createParam,int maxIdle, int initIdleCapacity) throws Exception {
        PoolableParent factory = (PoolableParent) poolableClass.newInstance();
        factory.setCreateParam(createParam);
       //StackObjectPool(int maxIdle, int initIdleCapacity)
        pool = new StackObjectPool(factory,maxIdle,initIdleCapacity);
    }

    public <T> T borrowObject() throws Exception {
        return (T) pool.borrowObject();
    }

    public <T> void returnObject(T t) throws Exception {
        pool.returnObject(t);
    }

    public void close() throws Exception {
        pool.close();
    }

    public void clear() throws Exception {
        pool.clear();
    }

    public int getIdelNum() {
        return pool.getNumIdle();
    }

    public int getActiveNum() {
        return pool.getNumActive();
    }
}
